var lipstickCfg = require("./cfg/lipstick.json")
var async = require("async")
//单人游戏管理器
module.exports = function(app) {
	console.log("solitaryManager")
	var manager = {}
	var gamesCfg = {
		"lipstick" : {}
	}
	var gamesInfo = {}  		//游戏状态记录表
	manager.init = function() {
		//读取初始配置
		for(let i = 0;i < lipstickCfg.length;i++){
			manager.remote.dbService.db.hgetall("game:lipstick:"+i,function(err,data) {
				if(!err && data){
					gamesCfg["lipstick"][i] = data
				}
				// console.log(gamesCfg["lipstick"])
			})
		}
	}
	manager.setRemote = function(remote) {
		manager.remote = remote
	}
	//设置游戏配置
	manager.setGameCfg = function(uid,params,cb) {
		manager.remote.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
			console.log("data : "+data)
			if(data >= 5){
				var gameType = params.gameType
				var index = params.index
				var key = params.key
				var value = params.value
				if(gamesCfg[gameType] && gamesCfg[gameType][index] && gamesCfg[gameType][index][key] != undefined){
					gamesCfg[gameType][index][key] = value
					manager.remote.dbService.db.hset("game:"+gameType+":"+index,key,value,function(err,data) {
						if(err){
							cb(false,"数据库异常")
						}else{
							cb(true)
						}
					})
				}else{
					cb(false,"参数错误")
				}
			}else{
				cb(false)
			}
		})
	}
	//获取游戏配置
	manager.getGameCfg = function(uid,params,cb) {
		gameType = params.gameType
		if(gamesCfg[gameType]){
			cb(true,gamesCfg[gameType])
		}else{
			cb(false)
		}
	}
	//开始游戏
	manager.signUp = function(uid,params,cb) {
		var gameType = params.gameType
		var index = params.index
		if(!gamesCfg[gameType] || gamesCfg[gameType][index] == undefined){
			cb(false)
			return
		}
		if(gamesInfo[uid]){
			manager.settlement(uid,{winFlag : false},function() {
				manager.signUp(uid,params,cb)
			})
			return
		}
		var needItemId = parseInt(gamesCfg[gameType][index].needItemId)
		var needValue = parseInt(gamesCfg[gameType][index].needValue)
		async.waterfall([
			function(next) {
				if(!needItemId){
					next()
					return
				}
				//判断报名条件
				manager.remote.dbService.getItem(uid,needItemId,function(data) {
					if(data === false || data < needValue){
						cb(false,"未到达报名条件")
					}else{
						next()
					}
				})
			},
			function(next) {
				//扣除费用
				if(!needValue){
					next()
					return
				}
				//扣除报名费
				manager.remote.app.rpc.db.remote.addItemReason(null,uid,needItemId,-needValue,"游戏报名:"+gameType+":"+gamesCfg[gameType][index].itemName,function(flag) {
					if(flag){
						next()
					}else{
						cb(false)
					}
				})
			},
			function() {
				gamesInfo[uid] = {
					beginTime : (new Date()).getTime(),
					gameType : gameType,
					index : index,
					levels : [3,4,5]
				}
				if(gamesCfg[gameType][index].inventory > 100 || (gamesCfg[gameType][index].inventory > 0 && Math.random() < gamesCfg[gameType][index].probability)){
					gamesInfo[uid].canWin = true
				}else{
					gamesInfo[uid].canWin = false
				}
				cb(true,gamesInfo[uid])
			}
		],function(err) {
			cb(false)
			console.log(err)
		})
	}
	//游戏结果
	manager.settlement = function(uid,params,cb) {
		if(!gamesInfo[uid]){
			cb(false,"不在游戏中")
			return
		}
		var gameType = gamesInfo[uid].gameType
		var index = gamesInfo[uid].index
		var winFlag = params.winFlag
		

		if(winFlag){
			if(gamesInfo[uid].canWin){
				//获得奖励
				var awardItemId = parseInt(gamesCfg[gameType][index].awardItemId)
				var awardValue = parseInt(gamesCfg[gameType][index].awardValue)
				if(awardItemId && awardValue){
					delete gamesInfo[uid]
					manager.remote.app.rpc.db.remote.addItemReason(null,uid,awardItemId,awardValue,"游戏成功:"+gameType+":"+gamesCfg[gameType][index].itemName,function(flag) {
						cb(flag,{awardItemId : awardItemId,awardValue : awardValue})
					})
					var changeInventory = parseInt(1 / gamesCfg[gameType][index].probability)
					gamesCfg[gameType][index].inventory -= changeInventory
					manager.remote.dbService.db.hincrby("game:"+gameType+":"+index,"inventory",-changeInventory,function(){})
				}
			}else{
				gamesCfg[gameType][index].inventory++
				manager.remote.dbService.db.hincrby("game:"+gameType+":"+index,"inventory",1,function(){})
				console.log("数据异常，客户端疑似被破解")
				delete gamesInfo[uid]
				cb(true)
			}
		}else{
			gamesCfg[gameType][index].inventory++
			manager.remote.dbService.db.hincrby("game:"+gameType+":"+index,"inventory",1,function(){})
			delete gamesInfo[uid]
			cb(true)
		}
	}
	app.set("solitaryManager",manager)
	return manager
}